package com.harmontronics.hanma.paas.paas.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmFolderAppService;
import com.harmontronics.hanma.paas.paas.dto.output.HmFolderResult;
import com.harmontronics.hanma.paas.paas.dto.input.HmFolderCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmFolderUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmFolder;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 流程文件夹
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-02 10:54:59
 */
@RestController
@RequestMapping("/api/hm-folder")
@Api(tags = {"流程文件夹"})
public class HmFolderController {
    @Autowired
    private HmFolderAppService hmFolderappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmFolderResult>> list() {
        List<HmFolder> hmFolderList = hmFolderappService.getList();

        return ResponseEntity.ok(Convert.toList(HmFolderResult.class, hmFolderList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmFolderResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                         @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmFolder> hmFolderPageBase = hmFolderappService.getList(pageNo, pageSize);
        PageBase<HmFolderResult> resultPageBase = PageBase.copy(hmFolderPageBase, HmFolderResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmFolderResult> info(@RequestParam("id") Long id) {
        HmFolder hmFolder = hmFolderappService.findById(id);
        HmFolderResult result = BeanUtil.toBean(hmFolder, HmFolderResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmFolderCreateInput hmFolderInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmFolder hmFolder = BeanUtil.toBean(hmFolderInput, HmFolder.class);
        hmFolderappService.save(hmFolder);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmFolderUpdateInput hmFolderInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmFolder hmFolder = BeanUtil.toBean(hmFolderInput, HmFolder.class);
        hmFolderappService.update(hmFolder);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmFolderappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

    /**
     * 检验字段是否重复
     *
     * @param field
     * @param value
     * @param id
     * @return
     */
    @GetMapping("valid-field")
    @ApiOperation("字段重复检查")
    public ResponseEntity<String> validField(@RequestParam String field, @RequestParam String value, @RequestParam(required = false) Long id) {
        hmFolderappService.validField(id, field, value);
        return ResponseEntity.ok();
    }
}
